package cn.hhchat.wp.controller;

import cn.hhchat.wp.config.WeeklyConfig;
import cn.hhchat.wp.dto.msg.RMsg;
import cn.hhchat.wp.dto.msg.RMsgFactory;
import cn.hhchat.wp.entity.ConfigEntity;
import cn.hhchat.wp.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created this one by HMH on 2017/10/22.
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    @PostMapping("/switchReg")
    public RMsg switchReg(Boolean isAllow){
        ConfigEntity config = configService.switchReg(isAllow);
        return RMsgFactory.ok(config);
    }

    @PostMapping("/switchLogin")
    public RMsg switchLogin(Boolean isAllow){
        ConfigEntity config = configService.switchLogin(isAllow);
        return RMsgFactory.ok(config);
    }

    @GetMapping("/getAllConfig")
    public RMsg getAllConfig() {
        Map<String, ConfigEntity> map;
        List<ConfigEntity> configEntities = configService.getAll();
        map = configEntities.stream().collect(Collectors.toMap(ConfigEntity::getName, c -> c));
        return RMsgFactory.ok(map);
    }


    @Autowired
    private ConfigService configService;

}
